package com.tma.demo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MultiSelectionListActivity extends Activity 
    implements OnClickListener, OnItemClickListener, OnItemLongClickListener {

    private static final String TAG = "MultiSelectionListActivity";
    private ListView mListView;
    private ArrayAdapter<String> mAdapter;
    private TextView mTitleTextView;
    private Button mRestartButton;
    private Button mDeleteButton;

    /**
     * Data for list view
     */
    private static final String[] MONTHS = { "January", "February", "March", "April",
            "May", "June", "July", "August", "September", "October",
            "November", "December" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_multi_selection_list);

        mListView = (ListView) findViewById(R.id.lv_month);
        mDeleteButton = (Button) findViewById(R.id.btn_delete);
        mRestartButton = (Button) findViewById(R.id.btn_restart);
        mTitleTextView = (TextView) findViewById(R.id.tv_title);

        List<String> months = buildData();
        mAdapter = new ArrayAdapter<String>(
                this,
                // the child view layout for ArrayAdapter must be a TextView
                android.R.layout.simple_list_item_multiple_choice,
                // data for list view
                // use list if we want to modify (add, remove) items from list view
                months);

        //mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MONTHS);

        mListView.setAdapter(mAdapter);

        // We must use android.R.layout.simple_list_item_multiple_choice 
        // in Adapter's constructor to use this method.
        // If we want to set choice mode to ListView.CHOICE_MODE_SINGLE, 
        // we should use layout android.R.layout.simple_list_item_single_choice
        mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);

        mDeleteButton.setOnClickListener(this);
        mRestartButton.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_array_adapter, menu);
        return true;
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_delete:
            processDelete();
            break;
        case R.id.btn_restart:
            repopulate();
        default:
            break;
        }
    }

    public boolean onItemLongClick(AdapterView<?> adapterView, View target, int position, long id) {
        // see layout definition of android.R.layout.simple_list_item_multiple_choice
        CheckedTextView ctv = (CheckedTextView) target;
        Log.i(TAG, "onItemLongClick: " + ctv.getText().toString());
        Log.i(TAG, "onItemLongClick: position = " + position + "; id = " + id);
        Toast.makeText(this, "onItemLongClick: position = " + position + "; id = " + id, Toast.LENGTH_SHORT).show();
        return true;
    }

    public void onItemClick(AdapterView<?> adapterView, View target, int position, long id) {
        // see layout definition of android.R.layout.simple_list_item_multiple_choice
        CheckedTextView ctv = (CheckedTextView) target;
        Log.i(TAG, "onItemClick: " + ctv.getText().toString());
        Log.i(TAG, "onItemClick: position = " + position + "; id = " + id);
    }

    private void processDelete() {
        int count = mListView.getCount();
        SparseBooleanArray checkedPositions = mListView.getCheckedItemPositions();
        Set<String> removedSet = new HashSet<String>();

        for (int i = 0; i < count; i++) {
            if (checkedPositions.get(i)) {
                // remove check selection
                checkedPositions.delete(i);
                String s = (String) mListView.getItemAtPosition(i);
                removedSet.add(s);
            }
        }

        for (String s : removedSet) {
            Log.i(TAG, "remove " + s);
            // call remove() on adapter will cause the list view 
            // remove the associated child view (row)
            mAdapter.remove(s);
        }

        if (mListView.getCount() == 0) {
            mDeleteButton.setEnabled(false);
            mTitleTextView.setText(getString(R.string.oops));
            mRestartButton.setVisibility(View.VISIBLE);
        }
    }

    private void repopulate() {
        List<String> data = buildData();
        for (String s : data) {
            // call add() on adapter will cause the list view 
            // add new child view (row)
            mAdapter.add(s);
        }

        mDeleteButton.setEnabled(true);
        mRestartButton.setVisibility(View.GONE);
        mTitleTextView.setText(getString(R.string.question));
    }

    private List<String> buildData() {
        List<String> data = new ArrayList<String>();
        for (String month : MONTHS) {
            data.add(month);
        }
        return data;
    }
}
